package cn.gdcp.lkqapp12;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.search.busline.BusLineResult;
import com.baidu.mapapi.search.busline.BusLineSearch;
import com.baidu.mapapi.search.busline.BusLineSearchOption;
import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiDetailSearchResult;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRoutePlanOption;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String TAG = "MainActivity";
    private Button btnPoiCity;
    private Button btnPoiMsg;
    private Button btnPoiNearby;
    private Button btnPoiBound;
    private Button btnPoiSug;
    private PoiSearch poiSearch;
    private MapView mapView = null;
    private BaiduMap baiduMap;
    private LatLng mlatLng;
    private AutoCompleteTextView autoTxt;
    private SuggestionSearch suggestionSearch;
    private ArrayList<String> stringArrayList = new ArrayList<>();
    private ArrayAdapter<String> arrayAdapter;
    private String key;
    private TextWatcher watcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mapView = findViewById(R.id.bmapView);
        baiduMap = mapView.getMap();
        autoTxt = findViewById(R.id.auto_txt);

        suggestionSearch = SuggestionSearch.newInstance();
        suggestionSearch.setOnGetSuggestionResultListener(new OnGetSuggestionResultListener() {
            @Override
            public void onGetSuggestionResult(SuggestionResult suggestionResult) {
                List<SuggestionResult.SuggestionInfo> suggestions = suggestionResult.getAllSuggestions();
                if (suggestions == null){
                    return;
                }
                stringArrayList.clear();
                for (int i =0; i<suggestions.size(); i++){
                    Log.e(TAG, suggestions.get(i).toString());
                    stringArrayList.add(suggestions.get(i).getKey());
                }

                arrayAdapter = new ArrayAdapter<>(MainActivity.this,android.R.layout.simple_list_item_1, stringArrayList);

                autoTxt.setAdapter(arrayAdapter);
                autoTxt.removeTextChangedListener(watcher);
                autoTxt.setText(key);
                autoTxt.setSelection(key.length());
                autoTxt.addTextChangedListener(watcher);
            }
        });

        watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                key = s.toString();
                suggestionSearch.requestSuggestion(new SuggestionSearchOption().city("广州").citylimit(true).keyword(key));
            }
        };
        autoTxt.addTextChangedListener(watcher);



        baiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                Log.e(TAG, latLng.toString());
                mlatLng = latLng;
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });

        baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(MainActivity.this, String.valueOf(marker.getExtraInfo().getInt("index")), Toast.LENGTH_SHORT).show();
                return false;
            }
        });


        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                Log.e(TAG, "onGetPoiResult = " + poiResult.getAllPoi().size());
                for (int i = 0; i < poiResult.getAllPoi().size(); i++) {
                    PoiInfo poiInfo = poiResult.getAllPoi().get(i);
                    Log.e(TAG, poiInfo.getName() + " " + poiInfo.getAddress() + " uid" + poiInfo.getUid());
                }
                //创建PoiOverlay对象
                PoiOverlay poiOverlay = new PoiOverlay(baiduMap);
                //设置Poi检索数据
                poiOverlay.setData(poiResult);
                //将poiOverlay添加至地图并缩放至合适级别
                poiOverlay.addToMap();
                poiOverlay.zoomToSpan();
            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {
                Log.e(TAG, poiDetailSearchResult.getPoiDetailInfoList().get(0).getDetailUrl());
            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });

        btnPoiCity = findViewById(R.id.btn_poiCity);
        btnPoiCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                poiSearch.searchInCity(new PoiCitySearchOption().city("广州").keyword("学校"));
            }
        });

        btnPoiMsg = findViewById(R.id.btn_poiMsg);
        btnPoiMsg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //a45f647e846847649a0050da
                poiSearch.searchPoiDetail(new PoiDetailSearchOption().poiUids("a45f647e846847649a0050da"));
            }
        });


        btnPoiNearby = findViewById(R.id.btn_poiNearby);
        btnPoiNearby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                poiSearch.searchNearby(new PoiNearbySearchOption().radius(1000).location(mlatLng).keyword("学校"));
            }
        });

        btnPoiBound = findViewById(R.id.btn_poiBound);
        btnPoiBound.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LatLng leftTop = new LatLng(mlatLng.latitude + 0.05, mlatLng.longitude + 0.05);
                LatLng rightBottom = new LatLng(mlatLng.latitude - 0.05, mlatLng.longitude - 0.05);
                LatLngBounds latLngBounds = new LatLngBounds.Builder().include(leftTop).include(rightBottom).build();
                poiSearch.searchInBound(new PoiBoundSearchOption().bound(latLngBounds).keyword("学校"));
            }
        });


        btnPoiSug = findViewById(R.id.btn_poiSug);
        btnPoiSug.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //检索公交信息
                /*searchBus();*/
                routePlan();
            }
        });

    }

    private void routePlan() {
        RoutePlanSearch routePlanSearch = RoutePlanSearch.newInstance();
        routePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {

            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
                Log.e(TAG, "size = " + transitRouteResult.getRouteLines().size());
                //创建TransitRouteOverlay实例
                TransitRouteOverlay transitRouteOverlay = new TransitRouteOverlay(baiduMap);
                //获取路径规划数据,(以返回的第一条数据为例)
                if (transitRouteResult.getRouteLines().size() > 0) {
                    transitRouteOverlay.setData(transitRouteResult.getRouteLines().get(0));
                    //在地图上绘制TransitRouteOverlay
                    transitRouteOverlay.addToMap();
                    transitRouteOverlay.zoomToSpan();
                }

            }

            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {

            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {

            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }

            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {

            }
        });

        routePlanSearch.transitSearch(new TransitRoutePlanOption().city("广州")
                .from(PlanNode.withCityNameAndPlaceName("广州","广东交通职业技术学院南校区"))
                .to(PlanNode.withCityNameAndPlaceName("广州","天平架"))
        );
    }

    private void searchBus() {

        PoiSearch poiSearch2 = PoiSearch.newInstance();
        poiSearch2.setOnGetPoiSearchResultListener(new OnGetPoiSearchResultListener() {
            @Override
            public void onGetPoiResult(PoiResult poiResult) {
                Log.e(TAG, poiResult.getAllPoi().get(0).name + " uid = " + poiResult.getAllPoi().get(0).getUid());
                searchBusDetail(poiResult.getAllPoi().get(0).getUid());

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

            }

            @Override
            public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

            }

            @Override
            public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

            }
        });
        poiSearch2.searchInCity(new PoiCitySearchOption().city("广州").keyword("84"));
    }

    private void searchBusDetail(String uid) {
        BusLineSearch busLineSearch = BusLineSearch.newInstance();
        busLineSearch.setOnGetBusLineSearchResultListener(new OnGetBusLineSearchResultListener() {
            @Override
            public void onGetBusLineResult(BusLineResult busLineResult) {
                BusLineOverlay busLineOverlay = new BusLineOverlay(baiduMap);
                busLineOverlay.setData(busLineResult);
                busLineOverlay.addToMap();
                busLineOverlay.zoomToSpan();
            }
        });
        busLineSearch.searchBusLine(new BusLineSearchOption().city("广州").uid(uid));

    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
    }
}
